-- resolve_spells
-- create by cheny3
-- 分解卷轴

return {
    -- 判断
    canTrigger = function(args)
        -- 获取玩家当前所有卷轴
        local spells = TestFightM.getUserSpells();
        if #spells <= 0 then
            -- 没卷轴
            return false;
        end

        local cur = ME.user:queryAttrib("mp");
        local max = ME.user:queryAttrib("max_mp");

        -- 魔法值降低到临界才进行分解，先50%吧
        if cur * 100 > max * 50 then
            return false;
        end

        return true;
    end,

    -- 触发
    trigger = function(args)
        local chanceRange = args["chance"];
        if chanceRange then
            if math.random(chanceRange[2]) > chanceRange[1] then
                -- 概率没触发
                return false;
            end
        end

        local resolveType = args["type"] or "one";
        local doTimes = args["count"];
        local spells  = TestFightM.getUserSpells();
        for i = 1, doTimes do
            if #spells <= 0 then
                -- 没卷轴了
                break;
            end

            -- 随机分解一件
            local randomNum = math.random(#spells);
            local classId = spells[randomNum];
            table.remove(spells, randomNum);
            local num = ItemM.getAmount(ME.user, classId);
            if resolveType == "one" then
                num = 1;
            end

            -- 分解卷轴
            DungeonActionM.go("split_spell", classId, num);
        end
        return true;
    end,
};
